package com.binaryTree;

import java.util.*;

public class BinaryTreeLevelOrderTraversalII0207_107 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        if(root == null) return new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        Queue<TreeNode> que = new LinkedList<>();
        que.offer(root);

        while (!que.isEmpty()){
            ArrayList<Integer> item = new ArrayList<>();
            int len = que.size();
            while (len>0){
                TreeNode top = que.poll();
                item.add(top.val);
                if(top.left != null) que.offer(top.left);
                if(top.right != null) que.offer(top.right);
                len--;
            }
            res.add(item);
        }
        Collections.reverse(res);

        return res;
    }
}
